package com.smz.service.impl;


import com.smz.dao.UserDAO;
import com.smz.entity.User;
import com.smz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;



    @Override
    public User selectUserByName(String username) {
        return userDAO.selectUserByName(username);
    }

    @Override
    public List<User> selectAllUsers() {
        return userDAO.selectAllUsers();
    }

    @Override
    public void addUser(User user) {
        String encode = new BCryptPasswordEncoder().encode(user.getPassword());
        System.out.println(user.getPassword());
        user.setPassword(encode);
        userDAO.insertUser(user);
    }

    @Override
    public void updateUser(User user) {
        String encode = new BCryptPasswordEncoder().encode(user.getPassword());
        System.out.println(user.getPassword());
        user.setPassword(encode);
        userDAO.updateUser(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userDAO.deleteUser(id);
    }

    @Override
    public User findById(Integer id) {
        return userDAO.selectById(id);
    }
}
